package com.jk.lzy.service.Impl;

import com.jk.CinemaBean;
import com.jk.OrderBean;
import com.jk.TreeBean;
import com.jk.lzy.dao.MovieInfoDao;
import com.jk.lzy.service.MovieSerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: 梁展艺
 * @Description:  业务层
 * @Date: 2021/5/19 16:33
 * @param null
 * @Return:
 **/
@Service
public class MovieInfoServiceImpl implements MovieSerService {
    // 注入 dao层
    @Autowired
    private MovieInfoDao lzyMovieDao;
    // 注入 mongodb
    @Autowired
    private MongoTemplate mongoTemplate;
    // 注入session
    @Autowired
    private HttpServletRequest httpServletRequest;

    @Override
    public HashMap<String, Object> eCharByOrder(String[] dateseven) {
        CinemaBean cinema = (CinemaBean) httpServletRequest.getSession().getAttribute("user");
        // 获取 session中的 影院的 id
        Integer cinemaId = cinema.getCinemaId();
        String startDate = dateseven[0];
        String endDate = dateseven[dateseven.length-1];
        // 定义 一个数组(收入额)
        double [] orderSum = new double[dateseven.length];
        // 定义 一个数组(订单数)
        double [] orderCount = new double[dateseven.length];
        // 定义一个 list近期收入额
        List<OrderBean> list = new ArrayList<>();
        Query query1 = new Query();
        List<OrderBean> orderBeans = mongoTemplate.find(query1, OrderBean.class);
        for (int i = 0; i < dateseven.length; i++) {
            // 定义 一个 总数
            double dou = 0;
            Query query = new Query();
            query.addCriteria(Criteria.where("cinemaId").is(cinemaId));
            query.addCriteria(Criteria.where("date").regex(dateseven[i]));
            list = mongoTemplate.find(query, OrderBean.class);
            for (OrderBean orderBean :list) {
                if (orderBean.getDate().contains(dateseven[i])){
                    dou += orderBean.getOrderPrice();
                }
            }
            orderSum[i] = dou;
            orderCount[i] = list.size();
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderSum",orderSum);
        map.put("orderCount",orderCount);
        return map;
    }
    // 管理员树页面
    @Override
    public List<TreeBean> findTreeRole() {
        Integer pid = -1;
        List<TreeBean> list = findTreePid(pid);
        return list;
    }
    private List<TreeBean> findTreePid(Integer pid) {
        List<TreeBean> list = lzyMovieDao.findTreeRolePid(pid);
        for (TreeBean treeBean : list) {
            Integer id = treeBean.getId();
            List<TreeBean> list1=findTreePid(id);
            if (list1!=null && list1.size()>0){
                treeBean.setSelectable(false);
                treeBean.setNodes(list1);
            }else{
                treeBean.setSelectable(true);
            }
        }
        return list;
    }
    /**
     * @Author: 梁展艺
     * @Description: 订单信息查询
     * @Date: 2021/5/20 16:11
     * @param cinemaId
     * @Return: java.util.List<com.jk.OrderBean>
     **/
    @Override
    public List<OrderBean> findOrderBy(OrderBean order) {
        CinemaBean cinema = (CinemaBean) httpServletRequest.getSession().getAttribute("user");
        // 获取 session中的 影院的 id
        Integer cinemaId = cinema.getCinemaId();
        Query query = new Query();
        query.addCriteria(Criteria.where("cinemaId").is(cinemaId));
        if (!StringUtils.isEmpty(order.getPhone())){
            query.addCriteria(Criteria.where("phone").is(order.getPhone()));
        }
        if (!StringUtils.isEmpty(order.getStartDate()) && !StringUtils.isEmpty(order.getEndDate())){
            query.addCriteria(Criteria.where("date").gte(order.getStartDate()).lte(order.getEndDate()));
        }
        if (!StringUtils.isEmpty(order.getStartDate())){
            query.addCriteria(Criteria.where("date").gte(order.getStartDate()));
        }else if (!StringUtils.isEmpty(order.getEndDate())){
            query.addCriteria(Criteria.where("date").lte(order.getEndDate()));
        }
        List<OrderBean> list = mongoTemplate.find(query, OrderBean.class);
        return list;
    }
}
